﻿using ProTrack.Entity;
using ProTrack.IRepository;
using ProTrack.IServices.StoreHouse;
using ProTrack.IServices.Works;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace ProTrack.Services.StoreHouse
{
    public class WmWarehouseAreaServices : IWmWarehouseAreaServices
    {
        public WmWarehouseAreaServices (IBaseRepository db)
        {
            Db = db;
        }
        public IBaseRepository Db { get; }

        public async Task<bool> AddAsync(WmWarehouseArea model)
        {
            var result = await Db.InsertAsync(model);
            return result;
        }

        public async Task<bool> DeleteAsync(int[] Ids)
        {
            string sql = "delete WmWarehouseArea where Id in @Ids";
            var result = await Db.DeleteAsync(sql, new { Ids });
            return result;
        }

        public async Task<bool> DeleteAsync(int Id)
        {
            string sql = "delete WmWarehouseArea where Id = @Id";
            var result = await Db.DeleteAsync(sql, new { Id });
            return result;
        }

        public async Task<IEnumerable<WmWarehouseArea>> GetAllAsync()
        {
            string sql = "select * from WmWarehouseArea ";
            var list = await Db.QueryAllAsync<WmWarehouseArea>(sql);
            return list;
        }

        public async Task<IEnumerable<WmWarehouseArea>> GetAsync(int PageIndex, int PageSize, string AreaNumber, string AreaName)
        {
            string sql = "select * from WmWarehouseArea where 1=1 ";
            if (!string.IsNullOrEmpty(AreaNumber))
            {
                sql += " and AreaNumber like @AreaNumber ";
            }

            if (!string.IsNullOrEmpty(AreaName))
            {
                sql += " and AreaName like @AreaName ";
            }

            sql += " order by CreateTime desc offset @PageSize*(@PageIndex-1) row fetch next @PageSize rows only ";



            var list = await Db.QueryAsync<WmWarehouseArea>(sql, new { PageIndex, PageSize, AreaNumber = $"%{AreaNumber}%", AreaName = $"%{AreaName}%" });
            return list;
        }

        public async Task<int> GetCountAsync(string AreaNumber, string AreaName)
        {
            string sql = "select count(*) from WmWarehouseArea where 1=1 ";
            if (!string.IsNullOrEmpty(AreaNumber))
            {
                sql += " and AreaNumber like @AreaNumber ";
            }

            if (!string.IsNullOrEmpty(AreaName))
            {
                sql += " and AreaName like @AreaName ";
            }

            var count = await Db.GetRecoreCountAsync(sql, new { AreaNumber = $"%{AreaNumber}%", AreaName = $"%{AreaName}%" });
            return count;
        }

        public async Task<bool> UpdateAsync(WmWarehouseArea model)
        {
            var result = await Db.UpdateAsync(model);
            return result;
        }
    }
}
